package com.chuangjiangx.member.score.ddd.domain.model;

import com.chuangjiangx.dddbase.Entity;
import com.chuangjiangx.dddbase.domain.shared.Timestamp;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.basic.ddd.domain.model.calc.AmountScoreGrandTotalCalc;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleId;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleScope;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleType;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import lombok.Getter;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 积分累计规则
 */
@Getter
@ToString
public class MbrScoreGrandTotalRule extends Entity<MbrScoreGrandTotalRuleId> {

    /**
     * 商户id
     */
    private MerchantId merchantId;

    /**
     * 规则名称
     */
    private String name;

    /**
     * 规则类型
     */
    private MbrScoreGrandTotalRuleType type;

    /**
     * 规则对象
     */
    private MbrScoreGrandTotalRuleScope scope;

    /**
     * 累计条件
     */
    private BigDecimal amount;

    /**
     * 奖励积分值
     */
    private Long score;

    /**
     * 是否启用
     */
    private Enable enable;


    /**
     * 数据库持久化转换构造方法
     *
     * @param id         id
     * @param name       规则名称
     * @param type       规则类型
     * @param scope      规则对象
     * @param amount     金额
     * @param score      积分
     * @param enable     是否启用
     * @param createTime 创建时间
     * @param updateTime 更新时间
     */
    public MbrScoreGrandTotalRule(MbrScoreGrandTotalRuleId id, MerchantId merchantId,
                           String name, MbrScoreGrandTotalRuleType type, MbrScoreGrandTotalRuleScope scope,
                           BigDecimal amount, Long score,
                           Enable enable, Date createTime, Date updateTime) {
        setId(id);
        this.merchantId = merchantId;
        this.name = name;
        this.type = type;
        this.scope = scope;
        this.amount = amount;
        this.score = score;
        this.enable = enable;
        this.setTimestamp(new Timestamp(createTime,updateTime));
    }

//    /**
//     * 创建积分累计规则
//     */
//    public MbrScoreGrandTotalRule(MerchantId merchantId, BigDecimal amount, Long score) {
//        this.merchantId = merchantId;
//        this.name = "每消费" + amount + "元积" + score + "分";
//        this.amount = amount;
//        this.score = score;
//        this.createTime = new Date();
//        this.updateTime = new Date();
//        this.scope = MbrScoreGrandTotalRuleScope.ALL;
//        this.type = MbrScoreGrandTotalRuleType.PAY;
//        this.enable();
//    }
//
//    public MbrScoreGrandTotalRule(MerchantId merchantId, BigDecimal amount, Long score, Long id, MbrScoreGrandTotalRuleType type) {
//        this.merchantId = merchantId;
//        this.amount = amount;
//        this.score = score;
//        this.type = type;
//        if (id != null) {
//            setId(new MbrScoreGrandTotalRuleId(id));
//        }
//        this.name = "每消费" + amount + "元积" + score + "分";
//    }


    /**
     * 启用
     */
    public void enable() {
        if (this.enable == null || this.enable.equals(Enable.DISABLE)) {
            this.enable = Enable.ENABLE;
            this.update();
        }
    }

    /**
     * 积分累计修改参数
     */
    public void update( BigDecimal amount, Long score) {
        this.amount = amount;
        this.score = score;
        this.name = "每消费" + amount + "元积" + score + "分";
        this.update();
    }

    /**
     * 计算累计积分
     * @param amount
     * @return
     */
    public Long calcObtainScore(BigDecimal amount){

        return new AmountScoreGrandTotalCalc()
                .plusScoreCalc(amount,this.amount,this.score);
    }


}
